<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>GTD</title>
<jsp:include page="../../../public/header.jsp"/>
</head>
<body>
	<jsp:include page="../../../public/nav.jsp"/>
	<div>
		<div class="jumbotron" >
			<p style="text-align: center;">用户中心!</p>
		</div>
	</div>
	<!-- start左边菜单 -->
	<jsp:include page="../../../public/usersLeft.jsp"></jsp:include>
	<!-- end左边菜单 -->
      <div class="col-xs-9">
      		<ol class="breadcrumb">
			  <li><a href="/users/index.html">用户中心</a></li>
			  <li><a href="#">我的工具</a></li>
			  <li class="active">GTD</li>
			</ol>
			<a href="/users/gtdIdeaAdd.html" type="button" class="btn btn-success">添加项目(action)</a>
			<jsp:include page="public/nav.jsp"/>
			<table class="table" id="datas">
			  <thead>
			  	<th>#</th>
			  	<th align="center">名字</th>
			  	<th align="center">预计开始时间</th>
			  	<th align="center">预计结束时间</th>
			  	<th align="center">状态</th>
			  	<th align="center">操作</th>
			  </thead>
			  <tbody id="body">
			  	
			  </tbody>
			</table>
			<nav aria-label="Page navigation">
			  <ul id="page" class="pagination">
			  </ul>
			</nav>
      </div>
      <div class="clearfix"></div>
	<jsp:include page="../../../public/footer2.jsp"/>
</body>
	<script type="text/javascript">
		var cur = 1;
		var pageSize = 10;
		var total = 0;
		$(function(){
			function initDatas(){
				var url = "/users/gtd.html";
				cur = 1;
				var data = {cur:1,pageSize:pageSize};
				getDataList(url,data);
			}
			
			initDatas();
			$("#all").click(function(){
				console.log("all click!!!");
				initDatas();
			});
			$("#doing").click(function(){
				console.log("doing click!!!");
				var url = "/users/gtdDoing.html";
				var data = {doing:'T'};
				getDataList(url,data);
			});
			$("#done").click(function(){
				console.log("done click!!!");
				var url = "/users/gtdDone.html";
				var data = {done:'T'};
				getDataList(url,data);
			});
			$("#gone").click(function(){
				console.log("gone click!!!");
				var url = "/users/gtdGone.html";
				var data = {gone:'T'};
				getDataList(url,data);
			});
			$("#rabit").click(function(){
				console.log("rabit click!!!");
				var url = "/users/gtdRabit.html";
				var data = {status:'F'};
				getDataList(url,data);
			});
			
			//--------------------------按钮
		})
		function page(curr){
			var url = "/users/gtd.html";
			var data = {cur:curr,pageSize:pageSize};
			getDataList(url,data);
		}
		
		function getDataList(url,data){
			var params = {url:url,data:data,type:'post',dataType:'json',success:function(result){
				console.log(result);
				if(result.code == 0){
					initBody(result.models);
					var page = result.page;
					console.log(page);
					cur = page.cur;
					total = page.total;
					var pageContent = '<li> <a id="prev" aria-label="Previous"><span aria-hidden="true">&laquo;</span></a></li>';
					for( var i = (cur-2>0?cur-2:0); i < cur;i++){
						if(i == cur - 1){
							pageContent += ' <li class="active"><a onclick="page('+(i+1)+')">'+(i+1)+'</a></li>';
						}else
						pageContent += ' <li><a onclick="page('+(i+1)+')">'+(i+1)+'</a></li>';
					}
					if(cur < total){
						pageContent += ' <li><a onclick="page('+(cur+1)+')">'+(cur+1)+'</a></li>';
						for( var i = cur + 2; i < total && i<cur + 4;i++){
							pageContent += ' <li><a onclick="page('+(i+1)+')">'+(i+1)+'</a></li>';
						}
					}
					pageContent += '<li><a id="next" aria-label="Next"><span aria-hidden="true">&raquo;</span></a></li>';
					$("#page").html(pageContent);
					pageClick();
				}else{
					alert(result.msg);
				}
			},error:function(){
				alert("服务器正在维护！");
			}};
			$.ajax(params);
		}
		
		function initBody(datas){
			var body = "";
			for(var i = 0; i < datas.length;i++){
				var model = datas[i];
				var status = '<span class="label label-default">还未开始</span>';
				
				if(model.doing == 'T'){
					status = '<span class="label label-primary">正在进行</span>';
				}
				if(model.gone == "T"){
					status = '<span class="label label-warning">已经逾期</span>';
				}
				if(model.done == "T"){
					status = '<span class="label label-success">已经完成</span>';
				}
				body += "<tr>";
				body+="<td>"+i+"</td>";
			  	body+='<td><a href="/users/work/gtdCheck.html?id='+model.id+'">'+model.name+'</td>';
			  	if(model.startDate !== null && model.startDate !== undefined )
			  		body+='<td>'+new Date(model.startDate.time).toLocaleString().replace(/年|月/g, "-").replace(/日/g, " ")+'</td>';
		  		else{
		  			body+='<td></td>';
		  		}
			  	if(model.modifytime !== null && model.doing !== 'F' )
			  		body+='<td>'+new Date(model.modifytime.time).toLocaleString().replace(/年|月/g, "-").replace(/日/g, " ")+'</td>';
		  		else{
		  			body+='<td></td>';
		  		}
			  	if(model.endDate !== null && model.endDate !== undefined )	
			  		body+='<td>'+new Date(model.endDate.time).toLocaleString().replace(/年|月/g, "-").replace(/日/g, " ")+'</td>';
		  		else body+='<td></td>';
			  	body+='<td>'+status+'</td>';
			  	body+='<td><a href="/users/work/gtd/step/index.html?id='+model.id+'" class=" btn-default" aria-label="Left Align"><span class="glyphicon glyphicon-th" aria-hidden="true">分解</span></a>';
			  	body+='<a href="/users/work/gtdModify.html?id='+model.id+'" class=" btn-default" aria-label="Left Align"><span class="glyphicon glyphicon-pencil" aria-hidden="true">修改</span></a>';
			  	body+='<a onclick="del('+model.id+')"  class=" btn-default" aria-label="Left Align"><span class="glyphicon glyphicon-remove" aria-hidden="true">删除</span></a></td>';
			  	body+= "</tr>"
			  	
			}
			
			$("#body").html(body);
			
		}

		function del(id){
			var url = "/users/gtdDel.html";
			$.ajax({url:url,data:{id:id},
				dataType:"json",
				success:function(data){
				alert(data.msg);
				window.reload();
			}});
		}
		function pageClick(){
			$("#prev").click(function(){
				cur -= 1;
				if(cur <= 0){
					cur = 1;
				}else{
					var url = "/users/gtd.html";
					var data = {cur:cur,pageSize:pageSize};
					getDataList(url,data);
				}
			})
			$("#next").click(function(){
				cur += 1;
				if(total < cur){
					cur -= 1;
				}else{
					var url = "/users/gtd.html";
					var data = {cur:cur,pageSize:pageSize};
					getDataList(url,data);
				}
			})
		}
	</script>
</html>